
#include <WiFi.h>
#include <DNSServer.h>
#include <WebServer.h>
#include <Preferences.h>
#include "config.h"

Preferences preferences;
String PrefSSID, PrefPassword, cityCode;// 保存的WiFi信息
String pageTemplate;            // 页面模板
extern String cachedSSIDs[15];  // 声明为数组
extern int cachedSSIDCount;     // 缓存的SSID数量
extern char device_name[15];
const char* AP_NAME = "情侣对讲机";//Web配网模式下的AP-wifi名字


//暂时存储wifi账号密码
struct WiFi_Info {
  char ssid[32];
  char password[64];
};
char sta_ssid[32] = {0};
char sta_password[64] = {0};
char citycode[10] = {0};

// 动态生成SSID选项
String generateSSIDOptions() {
  String options;
  for(int i=0; i<cachedSSIDCount; i++) {
    options += "<option value=\"" + cachedSSIDs[i] + "\">";
  }
  return options;
}

const char* page_header = R"raw(
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网络配置</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <form class="card" name="input" action="/" method="POST">
        <!-- WiFi 名称 -->
        <input id="wifi-name" type="text" name="ssid" autocomplete="on" 
               placeholder="WiFi名称" list="ssidList">
        <datalist id="ssidList">
            <!-- 动态插入 WiFi 名称选项 -->
)raw";

const char* page_footer = R"raw(
        </datalist>

        <!-- WiFi 密码 -->
        <input id="wifi-password" type="password" name="password" placeholder="WiFi密码">

        <!-- 城市名称 -->
        <input id="city-name" type="text" name="cityname" placeholder="城市名称" 
               oninput="checkCity(this)">
        <input type="hidden" id="cityid" name="cityid">
        <!-- 设备名称 -->
        <input id="device-name" type="text" name="deviceName" placeholder="与另一半相同的设备名称" value=")raw";

const char* device_value = device_name ? device_name : "";
const char* page_footer_2 = R"raw(">

<!-- 二选一选择框 -->
<div class="gender-selection">
    <label class="gender-option left">
        <input type="radio" name="gender" value="0" required>
        <span>♂️ 先生</span>
    </label>
    <label class="gender-option right">
        <input type="radio" name="gender" value="1" required>
        <span>♀️ 小姐</span>
    </label>
</div>

        <!-- 全局提示 -->
        <div id="tip" class="tip">请输入或选择WiFi名称</div>

        <!-- 高级设置开关 -->
        <div class="advanced-toggle" onclick="toggleAdvancedSettings()">
            <button type="button">⯆ 高级设置（小白勿改）</button>
        </div>
        
        <!-- 高级设置内容 -->
        <div class="advanced-settings" style="display:none;">
            <input type="text" name="mqtt_url" placeholder="MQTT的URL" value=")raw";
const char* mqtt_url_value = MQTT_IP ? MQTT_IP : "broker-cn.emqx.io";
const char* page_footer_3 = R"raw(">
            <input type="number" name="port" placeholder="端口号" value=")raw";
std::string portStr = std::to_string(PORT);
const char* port_value = new char[portStr.size() + 1];
const char* page_footer_4 = R"raw(">
            <input type="text" name="mqtt_user" placeholder="MQTT用户名" value=")raw";
const char* mqtt_user_value = MQTT_USER ? MQTT_USER : "";
const char* page_footer_5 = R"raw(">
            <input type="password" name="mqtt_pass" placeholder="MQTT密码" value=")raw";
const char* mqtt_pass_value = MQTT_PASSWORD ? MQTT_PASSWORD : "";
const char* page_footer_6 = R"raw(">
        </div>

        <!-- 保存按钮 -->
        <button id="save-button" type="submit" disabled>保存</button>
    </form>
    <script src="index.js"></script>
</body>
</html>
)raw";

// style.css
String style_css = R"x(
* {
    padding: 0;
    margin: 0;
}

body {
    width: 100vw;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
}

body::before {
    content: "";
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    opacity: 0.3;
    z-index: 0;
    background-image:url('');
    background-repeat: none;
    background-size: cover;
    background-position: 50% 50%;
}

.tip {
    background-color: transparent;
    color: #fff;
    text-align: center;
}

.tip.error {
    color: red;
}

button:disabled {
    background-color: lightgray;
    cursor: not-allowed;
}
.gender-selection {
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin: .75rem 0;
}

.gender-option {
    display: inline-flex;
    align-items: center;
    cursor: pointer;
    color: #fff;
    background-color: rgba(255, 255, 255, 0.1);
    padding: 0.5rem 1rem;
    border-radius: 2rem;
    transition: background-color 0.3s ease;
}

.gender-option:hover {
    background-color: rgba(255, 255, 255, 0.2);
}

.gender-option input[type="radio"] {
    margin-right: 0.5rem;
}

.left {
    justify-content: flex-start;
}

.right {
    justify-content: flex-end;
}


.card {
    z-index: 1;
    background-color: rgba(0, 0, 0, 0.3);
    color: #fff;
    border: 0.25rem solid #fff;
    border-radius: 1rem;
    max-width: 90%;
    padding: 1rem;
    display: flex;
    flex-direction: column;
}

.card > * {
    margin: 0.75rem;
    border-radius: 2rem;
    font-size: 1.5rem;
}

input[type=text], input[type=password], .card > button {
    padding: 0.5rem 1.5rem;
    color: #fff;
    border: 0.125rem solid #fff;
    background-color: rgba(0, 0, 0, 0.5);
    -webkit-appearance: none;
    appearance: none;
}


.card > button {
    background-color: rgba(255, 255, 255, 0.5);
}

input[type=text]:focus, input[type=password]:focus, .card > button:focus {
    outline: 0.25rem solid #fff;
    outline-offset: -2px;
}

input[type=text]::placeholder, input[type=password]::placeholder {
    color: #aaa;
    text-align: right;
}

.advanced-toggle {
    margin: 15px 0;
    cursor: pointer;
    color: #666;
}

.advanced-toggle button {
    background: none;
    border: none;
    padding: 5px;
    font-size: 0.9em;
    color: #2c3e50;
    cursor: pointer;
}

.hint {
    font-size: 0.8em;
    margin-left: 8px;
}

.advanced-settings {
    border-top: 1px solid #eee;
    padding-top: 15px;
    margin-bottom: 20px;
}

.advanced-settings input {
    padding: 0.5rem 1.5rem;
    color: #fff;
    border: 0.125rem solid #fff;
    background-color: rgba(0, 0, 0, 0.5);
    -webkit-appearance: none;
    appearance: none;
    font-size: 1.5rem;
    margin-right: 0.5rem;
    border-radius: 2rem;
}

)x";


// index.js
//String index_js = R"x(
const char* index_js = R"rawliteral(
const cityIds = (() => {
    const result = {};
    `北京,010100;海淀,010200;朝阳,010300;顺义,010400;怀柔,010500;通州,010600;昌平,010700;延庆,010800;丰台,010900;石景山,011000;大兴,011100;房山,011200;密云,011300;门头沟,011400;平谷,011500;八达岭,011600;佛爷顶,011700;汤河口,011800;密云上甸子,011900;斋堂,012000;霞云岭,012100;上海,020100;闵行,020200;宝山,020300;川沙,020400;嘉定,020500;南汇,020600;金山,020700;青浦,020800;松江,020900;奉贤,021000;崇明,021100;陈家镇,021101;引水船,021102;徐家汇,021200;浦东,021300;天津,030100;武清,030200;宝坻,030300;东丽,030400;西青,030500;北辰,030600;宁河,030700;汉沽,030800;静海,030900;津南,031000;塘沽,031100;大港,031200;平台,031300;蓟县,031400;重庆,040100;永川,040200;合川,040300;南川,040400;江津,040500;万盛,040600;渝北,040700;北碚,040800;巴南,040900;长寿,041000;黔江,041100;万州天城,041200;万州龙宝,041300;涪陵,041400;开县,041500;城口,041600;云阳,041700;巫溪,041800;奉节,041900;巫山,042000;潼南,042100;垫江,042200;梁平,042300;忠县,042400;石柱,042500;大足,042600;荣昌,042700;铜梁,042800;璧山,042900;丰都,043000;武隆,043100;彭水,043200;綦江,043300;酉阳,043400;金佛山,043500;秀山,043600;沙坪坝,043700;哈尔滨,050101;双城,050102;呼兰,050103;阿城,050104;宾县,050105;依兰,050106;巴彦,050107;通河,050108;方正,050109;延寿,050110;尚志,050111;五常,050112;木兰,050113;齐齐哈尔,050201;讷河,050202;龙江,050203;甘南,050204;富裕,050205;依安,050206;拜泉,050207;克山,050208;克东,050209;泰来,050210;牡丹江,050301;海林,050302;穆棱,050303;林口,050304;绥芬河,050305;宁安,050306;东宁,050307;佳木斯,050401;汤原,050402;抚远,050403;桦川,050404;桦南,050405;同江,050406;富锦,050407;绥化,050501;肇东,050502;安达,050503;海伦,050504;明水,050505;望奎,050506;兰西,050507;青冈,050508;庆安,050509;绥棱,050510;黑河,050601;嫩江,050602;孙吴,050603;逊克,050604;五大连池,050605;北安,050606;大兴安岭,050701;塔河,050702;漠河,050703;呼玛,050704;呼中,050705;新林,050706;阿木尔,050707;加格达奇,050708;伊春,050801;乌伊岭,050802;五营,050803;铁力,050804;嘉荫,050805;大庆,050901;林甸,050902;肇州,050903;肇源,050904;杜蒙,050905;七台河,051002;勃利,051003;鸡西,051101;虎林,051102;密山,051103;鸡东,051104;鹤岗,051201;绥滨,051202;萝北,051203;双鸭山,051301;集贤,051302;宝清,051303;饶河,051304;长春,060101;农安,060102;德惠,060103;九台,060104;榆树,060105;双阳,060106;吉林,060201;舒兰,060202;永吉,060203;蛟河,060204;磐石,060205;桦甸,060206;烟筒山,060207;延吉,060301;敦化,060302;安图,060303;汪清,060304;和龙,060305;天池,060306;龙井,060307;珲春,060308;图们,060309;松江,060310;罗子沟,060311;延边,060312;四平,060401;双辽,060402;梨树,060403;公主岭,060404;伊通,060405;孤家子,060406;通化,060501;梅河口,060502;柳河,060503;辉南,060504;集安,060505;通化县,060506;白城,060601;洮南,060602;大安,060603;镇赉,060604;通榆,060605;辽源,060701;东丰,060702;松原,060801;乾安,060802;前郭,060803;长岭,060804;扶余,060805;白山,060901;靖宇,060902;临江,060903;东岗,060904;长白,060905;沈阳,070101;苏家屯,070102;辽中,070103;康平,070104;法库,070105;新民,070106;于洪,070107;新城子,070108;大连,070201;瓦房店,070202;金州,070203;普兰店,070204;旅顺,070205;长海,070206;庄河,070207;皮口,070208;海洋岛,070209;鞍山,070301;台安,070302;岫岩,070303;海城,070304;抚顺,070401;清原,070403;章党,070404;本溪,070501;本溪县,070502;草河口,070503;桓仁,070504;丹东,070601;凤城,070602;宽甸,070603;东港,070604;东沟,070605;锦州,070701;凌海,070702;北宁,070703;义县,070704;黑山,070705;北镇,070706;营口,070801;大石桥,070802;盖州,070803;阜新,070901;彰武,070902;辽阳,071001;辽阳县,071002;灯塔,071003;铁岭,071101;开原,071102;昌图,071103;西丰,071104;朝阳,071201;建平,071202;凌源,071203;喀左,071204;北票,071205;羊山,071206;建平县,071207;盘锦,071301;大洼,071302;盘山,071303;葫芦岛,071401;建昌,071402;绥中,071403;兴城,071404;呼和浩特,080101;土默特左旗,080102;托克托,080103;和林格尔,080104;清水河,080105;呼和浩特市郊区,080106;武川,080107;包头,080201;白云鄂博,080202;满都拉,080203;土默特右旗,080204;固阳,080205;达尔罕茂明安联合旗,080206;石拐,080207;乌海,080301;集宁,080401;卓资,080402;化德,080403;商都,080404;希拉穆仁,080405;兴和,080406;凉城,080407;察哈尔右翼前旗,080408;察哈尔右翼中旗,080409;察哈尔右翼后旗,080410;四子王旗,080411;丰镇,080412;通辽,080501;舍伯吐,080502;科尔沁左翼中旗,080503;科尔沁左翼后旗,080504;青龙山,080505;开鲁,080506;库伦旗,080507;奈曼旗,080508;扎鲁特旗,080509;高力板,080510;巴雅尔吐胡硕,080511;通辽钱家店,080512;赤峰,080601;赤峰郊区站,080602;阿鲁科尔沁旗,080603;浩尔吐,080604;巴林左旗,080605;巴林右旗,080606;林西,080607;克什克腾旗,080608;翁牛特旗,080609;岗子,080610;喀喇沁旗,080611;八里罕,080612;宁城,080613;敖汉旗,080614;宝过图,080615;鄂尔多斯,080701;达拉特旗,080703;准格尔旗,080704;鄂托克前旗,080705;河南,080706;伊克乌素,080707;鄂托克旗,080708;杭锦旗,080709;乌审旗,080710;伊金霍洛旗,080711;乌审召,080712;东胜,080713;临河,080801;五原,080802;磴口,080803;乌拉特前旗,080804;大佘太,080805;乌拉特中旗,080806;乌拉特后旗,080807;海力素,080808;那仁宝力格,080809;杭锦后旗,080810;巴盟农试站,080811;锡林浩特,080901;朝克乌拉,080902;二连浩特,080903;阿巴嘎旗,080904;伊和郭勒,080905;苏尼特左旗,080906;苏尼特右旗,080907;朱日和,080908;东乌珠穆沁旗,080909;西乌珠穆沁旗,080910;太仆寺旗,080911;镶黄旗,080912;正镶白旗,080913;正兰旗,080914;多伦,080915;博克图,080916;乌拉盖,080917;白日乌拉,080918;那日图,080919;呼伦贝尔,081000;海拉尔,081001;小二沟,081002;阿荣旗,081003;莫力达瓦旗,081004;鄂伦春旗,081005;鄂温克旗,081006;陈巴尔虎旗,081007;新巴尔虎左旗,081008;新巴尔虎右旗,081009;满洲里,081010;牙克石,081011;扎兰屯,081012;额尔古纳,081014;根河,081015;图里河,081016;乌兰浩特,081101;阿尔山,081102;科尔沁右翼中旗,081103;胡尔勒,081104;扎赉特旗,081105;索伦,081106;突泉,081107;霍林郭勒,081108;阿拉善左旗,081201;阿拉善右旗,081202;额济纳旗,081203;拐子湖,081204;吉兰太,081205;锡林高勒,081206;头道湖,081207;中泉子,081208;巴彦诺尔贡,081209;雅布赖,081210;乌斯太,081211;孪井滩,081212;石家庄,090101;井陉,090102;正定,090103;栾城,090104;行唐,090105;灵寿,090106;高邑,090107;深泽,090108;赞皇,090109;无极,090110;平山,090111;元氏,090112;赵县,090113;辛集,090114;藁城,090115;晋洲,090116;新乐,090117;保定,090201;满城,090202;阜平,090203;徐水,090204;唐县,090205;高阳,090206;容城,090207;紫荆关,090208;涞源,090209;望都,090210;安新,090211;易县,090212;涞水,090213;曲阳,090214;蠡县,090215;顺平,090216;雄县,090217;涿州,090218;定州,090219;安国,090220;高碑店,090221;张家口,090301;宣化,090302;张北,090303;康保,090304;沽源,090305;尚义,090306;蔚县,090307;阳原,090308;怀安,090309;万全,090310;怀来,090311;涿鹿,090312;赤城,090313;崇礼,090314;承德,090402;承德县,090403;兴隆,090404;平泉,090405;滦平,090406;隆化,090407;丰宁,090408;宽城,090409;围场,090410;塞罕坎,090411;唐山,090501;丰南,090502;丰润,090503;滦县,090504;滦南,090505;乐亭,090506;迁西,090507;玉田,090508;唐海,090509;遵化,090510;迁安,090511;廊坊,090601;固安,090602;永清,090603;香河,090604;大城,090605;文安,090606;大厂,090607;霸州,090608;三河,090609;沧州,090701;青县,090702;东光,090703;海兴,090704;盐山,090705;肃宁,090706;南皮,090707;吴桥,090708;献县,090709;孟村,090710;泊头,090711;任丘,090712;黄骅,090713;河间,090714;曹妃甸,090715;衡水,090801;枣强,090802;武邑,090803;武强,090804;饶阳,090805;安平,090806;故城,090807;景县,090808;阜城,090809;冀州,090810;深州,090811;邢台,090901;临城,090902;邢台县浆水,090903;内邱,090904;柏乡,090905;隆尧,090906;南和,090907;宁晋,090908;巨鹿,090909;新河,090910;广宗,090911;平乡,090912;威县,090913;清河,090914;临西,090915;南宫,090916;沙河,090917;任县,090918;邯郸,091001;峰峰,091002;临漳,091003;成安,091004;大名,091005;涉县,091006;磁县,091007;肥乡,091008;永年,091009;邱县,091010;鸡泽,091011;广平,091012;馆陶,091013;魏县,091014;曲周,091015;武安,091016;秦皇岛,091101;青龙,091102;昌黎,091103;抚宁,091104;卢龙,091105;北戴河,091106;太原,100101;清徐,100102;阳曲,100103;娄烦,100104;太原古交区,100105;太原北郊,100106;太原南郊,100107;大同,100201;阳高,100202;大同县,100203;天镇,100204;广灵,100205;灵邱,100206;浑源,100207;左云,100208;阳泉,100301;盂县,100302;平定,100303;晋中,100401;榆次,100402;榆社,100403;左权,100404;和顺,100405;昔阳,100406;寿阳,100407;太谷,100408;祁县,100409;平遥,100410;灵石,100411;介休,100412;长治,100501;黎城,100502;屯留,100503;潞城,100504;襄垣,100505;平顺,100506;武乡,100507;沁县,100508;长子,100509;沁源,100510;壶关,100511;晋城,100601;沁水,100602;阳城,100603;陵川,100604;高平,100605;临汾,100701;曲沃,100702;永和,100703;隰县,100704;大宁,100705;吉县,100706;襄汾,100707;蒲县,100708;汾西,100709;洪洞,100710;霍州,100711;乡宁,100712;翼城,100713;侯马,100714;浮山,100715;安泽,100716;古县,100717;运城,100801;临猗,100802;稷山,100803;万荣,100804;河津,100805;新绛,100806;绛县,100807;闻喜,100808;垣曲,100809;永济,100810;芮城,100811;夏县,100812;平陆,100813;朔州,100901;平鲁,100902;山阴,100903;右玉,100904;应县,100905;怀仁,100906;忻州,101001;定襄,101002;五台县豆村,101003;河曲,101004;偏关,101005;神池,101006;宁武,101007;代县,101008;繁峙,101009;五台山,101010;保德,101011;静乐,101012;岢岚,101013;五寨,101014;原平,101015;吕梁,101100;离石,101101;临县,101102;兴县,101103;岚县,101104;柳林,101105;石楼,101106;方山,101107;交口,101108;中阳,101109;孝义,101110;汾阳,101111;文水,101112;交城,101113;西安,110101;长安,110102;临潼,110103;蓝田,110104;周至,110105;户县,110106;高陵,110107;杨凌,110108;咸阳,110200;三原,110201;礼泉,110202;永寿,110203;淳化,110204;泾阳,110205;武功,110206;乾县,110207;彬县,110208;长武,110209;旬邑,110210;兴平,110211;延安,110300;延长,110301;延川,110302;子长,110303;宜川,110304;富县,110305;志丹,110306;安塞,110307;甘泉,110308;洛川,110309;黄陵,110310;黄龙,110311;吴起,110312;榆林,110401;府谷,110402;神木,110403;佳县,110404;定边,110405;靖边,110406;横山,110407;米脂,110408;子洲,110409;绥德,110410;吴堡,110411;清涧,110412;渭南,110501;华县,110502;潼关,110503;大荔,110504;白水,110505;富平,110506;蒲城,110507;澄城,110508;合阳,110509;韩城,110510;华阴,110511;华山,110512;商洛,110601;洛南,110602;柞水,110603;镇安,110605;丹凤,110606;商南,110607;山阳,110608;安康,110701;紫阳,110702;石泉,110703;汉阴,110704;旬阳,110705;岚皋,110706;平利,110707;白河,110708;镇坪,110709;宁陕,110710;汉中,110801;略阳,110802;勉县,110803;留坝,110804;洋县,110805;城固,110806;西乡,110807;佛坪,110808;宁强,110809;南郑,110810;镇巴,110811;宝鸡,110901;宝鸡县,110902;千阳,110903;麟游,110904;岐山,110905;凤翔,110906;扶风,110907;眉县,110908;太白,110909;凤县,110910;陇县,110911;铜川,111001;耀县,111002;宜君,111003;济南,120101;长清,120102;商河,120103;章丘,120104;平阴,120105;济阳,120106;青岛,120201;崂山,120202;潮连岛,120203;即墨,120204;胶州,120205;胶南,120206;莱西,120207;平度,120208;淄博,120301;淄川,120302;博山,120303;高青,120304;周村,120305;沂源,120306;桓台,120307;临淄,120308;德州,120401;武城,120402;临邑,120403;陵县,120404;齐河,120405;乐陵,120406;庆云,120407;平原,120408;宁津,120409;夏津,120410;禹城,120411;烟台,120501;莱州,120502;长岛,120503;蓬莱,120504;龙口,120505;招远,120506;栖霞,120507;福山,120508;牟平,120509;莱阳,120510;海阳,120511;千里岩,120512;潍坊,120601;青州,120602;寿光,120603;临朐,120604;昌乐,120605;昌邑,120606;安丘,120607;高密,120608;诸城,120609;济宁,120701;嘉祥,120702;微山,120703;鱼台,120704;兖州,120705;金乡,120706;汶上,120707;泗水,120708;梁山,120709;曲阜,120710;邹城,120711;泰安,120801;新泰,120802;泰山,120803;肥城,120804;东平,120805;宁阳,120806;临沂,120901;莒南,120902;沂南,120903;苍山,120904;临沭,120905;郯城,120906;蒙阴,120907;平邑,120908;费县,120909;沂水,120910;马站,120911;菏泽,121001;鄄城,121002;郓城,121003;东明,121004;定陶,121005;巨野,121006;曹县,121007;成武,121008;单县,121009;滨州,121101;博兴,121102;无棣,121103;阳信,121104;惠民,121105;沾化,121106;邹平,121107;东营,121201;河口,121202;垦利,121203;利津,121204;广饶,121205;威海,121301;文登,121302;荣成,121303;乳山,121304;成山头,121305;石岛,121306;枣庄,121401;薛城,121402;峄城,121403;台儿庄,121404;滕州,121405;日照,121501;五莲,121502;莒县,121503;莱芜,121601;聊城,121701;冠县,121702;阳谷,121703;高唐,121704;茌平,121705;东阿,121706;临清,121707;朝城,121708;莘县,121709;乌鲁木齐,130101;蔡家湖,130102;小渠子,130103;巴仑台,130104;达坂城,130105;十三间房气象站,130106;天山大西沟,130107;乌鲁木齐牧试站,130108;天池,130109;白杨沟,130110;克拉玛依,130201;石河子,130301;炮台,130302;莫索湾,130303;乌兰乌苏,130304;昌吉,130401;呼图壁,130402;米泉,130403;阜康,130404;吉木萨尔,130405;奇台,130406;玛纳斯,130407;木垒,130408;北塔山,130409;吐鲁番,130501;托克逊,130502;吐鲁番东坎,130503;鄯善,130504;红柳河,130505;库尔勒,130601;轮台,130602;尉犁,130603;若羌,130604;且末,130605;和静,130606;焉耆,130607;和硕,130608;库米什,130609;巴音布鲁克,130610;铁干里克,130611;博湖,130612;塔中,130613;阿拉尔,130701;阿克苏,130801;乌什,130802;温宿,130803;拜城,130804;新和,130805;沙雅,130806;库车,130807;柯坪,130808;阿瓦提,130809;喀什,130901;英吉沙,130902;塔什库尔干,130903;麦盖提,130904;莎车,130905;叶城,130906;泽普,130907;巴楚,130908;岳普湖,130909;伽师,130910;伊宁,131001;察布查尔,131002;尼勒克,131003;伊宁县,131004;巩留,131005;新源,131006;昭苏,131007;特克斯,131008;霍城,131009;霍尔果斯,131010;塔城,131101;裕民,131102;额敏,131103;和布克赛尔,131104;托里,131105;乌苏,131106;沙湾,131107;和丰,131108;哈密,131201;沁城,131202;巴里坤,131203;伊吾,131204;淖毛湖,131205;和田,131301;皮山,131302;策勒,131303;墨玉,131304;洛浦,131305;民丰,131306;于田,131307;阿勒泰,131401;哈巴河,131402;一八五团,131403;黑山头,131404;吉木乃,131405;布尔津,131406;福海,131407;富蕴,131408;青河,131409;安德河,131410;阿图什,131501;乌恰,131502;阿克陶,131503;阿合奇,131504;吐尔尕特,131505;博乐,131601;温泉,131602;精河,131603;阿拉山口,131606;拉萨,140101;当雄,140102;尼木,140103;墨竹贡卡,140104;日喀则,140201;拉孜,140202;南木林,140203;聂拉木,140204;定日,140205;江孜,140206;帕里,140207;山南,140301;贡嘎,140302;琼结,140303;加查,140304;浪卡子,140305;错那,140306;隆子,140307;泽当,140308;林芝,140401;波密,140402;米林,140403;察隅,140404;昌都,140501;丁青,140502;类乌齐,140503;洛隆,140504;左贡,140505;芒康,140506;八宿,140507;那曲,140601;嘉黎,140603;班戈,140604;安多,140605;索县,140606;比如,140607;阿里,140701;改则,140702;申扎,140703;狮泉河,140704;普兰,140705;西宁,150101;大通,150102;湟源,150103;湟中,150104;铁卜加,150105;铁卜加寺,150106;中心站,150107;海东,150201;乐都,150202;民和,150203;互助,150204;化隆,150205;循化,150206;冷湖,150207;平安,150208;黄南,150301;尖扎,150302;泽库,150303;河南,150304;海南,150401;江西沟,150402;贵德,150404;河卡,150405;兴海,150406;贵南,150407;同德,150408;共和,150409;果洛,150501;班玛,150502;甘德,150503;达日,150504;久治,150505;玛多,150506;清水河,150507;玛沁,150508;玉树,150601;托托河,150602;治多,150603;杂多,150604;囊谦,150605;曲麻莱,150606;海西,150701;格尔木,150702;察尔汉,150703;野牛沟,150704;五道梁,150705;小灶火,150706;天峻,150708;乌兰,150709;都兰,150710;诺木洪,150711;茫崖,150712;大柴旦,150713;茶卡,150714;香日德,150715;德令哈,150716;海北,150801;门源,150802;祁连,150803;海晏,150804;托勒,150805;刚察,150806;兰州,160101;皋兰,160102;永登,160103;榆中,160104;定西,160201;通渭,160202;陇西,160203;渭源,160204;临洮,160205;漳县,160206;岷县,160207;安定,160208;平凉,160301;泾川,160302;灵台,160303;崇信,160304;华亭,160305;庄浪,160306;静宁,160307;崆峒,160308;庆阳,160401;西峰,160402;环县,160403;华池,160404;合水,160405;正宁,160406;宁县,160407;镇原,160408;庆城,160409;武威,160501;民勤,160502;古浪,160503;乌鞘岭,160504;天祝,160505;金昌,160601;永昌,160602;张掖,160701;肃南,160702;民乐,160703;临泽,160704;高台,160705;山丹,160706;酒泉,160801;鼎新,160802;金塔,160803;马鬃山,160804;瓜州,160805;肃北,160806;玉门镇,160807;敦煌,160808;天水,160901;北道区,160902;清水,160903;秦安,160904;甘谷,160905;武山,160906;张家川,160907;麦积,160908;武都,161001;成县,161002;文县,161003;宕昌,161004;康县,161005;西和,161006;礼县,161007;徽县,161008;两当,161009;临夏,161101;康乐,161102;永靖,161103;广河,161104;和政,161105;东乡,161106;合作,161201;临潭,161202;卓尼,161203;舟曲,161204;迭部,161205;玛曲,161206;碌曲,161207;夏河,161208;白银,161301;靖远,161302;会宁,161303;华家岭,161304;景泰,161305;银川,170101;永宁,170102;灵武,170103;贺兰,170104;石嘴山,170201;惠农,170202;平罗,170203;陶乐,170204;石炭井,170205;大武口,170206;吴忠,170301;同心,170302;盐池,170303;韦州,170304;麻黄山,170305;青铜峡,170306;固原,170401;西吉,170402;隆德,170403;泾源,170404;六盘山,170405;彭阳,170406;中卫,170501;中宁,170502;兴仁堡,170503;海原,170504;郑州,180101;巩义,180102;荥阳,180103;登封,180104;新密,180105;新郑,180106;中牟,180107;郑州农试站,180108;安阳,180201;汤阴,180202;滑县,180203;内黄,180204;林州,180205;新乡,180301;获嘉,180302;原阳,180303;辉县,180304;卫辉,180305;延津,180306;封丘,180307;长垣,180308;许昌,180401;鄢陵,180402;襄城,180403;长葛,180404;禹州,180405;平顶山,180501;郏县,180502;宝丰,180503;汝州,180504;叶县,180505;舞钢,180506;鲁山,180507;信阳,180601;息县,180602;罗山,180603;光山,180604;新县,180605;淮滨,180606;潢川,180607;固始,180608;商城,180609;鸡公山,180610;信阳地区农试站,180611;南阳,180701;南召,180702;方城,180703;社旗,180704;西峡,180705;内乡,180706;镇平,180707;淅川,180708;新野,180709;唐河,180710;邓州,180711;桐柏,180712;开封,180801;杞县,180802;尉氏,180803;通许,180804;兰考,180805;洛阳,180901;新安,180902;孟津,180903;宜阳,180904;洛宁,180905;伊川,180906;嵩县,180907;偃师,180908;栾川,180909;汝阳,180910;商丘,181001;睢阳区,181002;睢县,181003;民权,181004;虞城,181005;柘城,181006;宁陵,181007;夏邑,181008;永城,181009;焦作,181101;修武,181102;武陟,181103;沁阳,181104;博爱,181106;温县,181107;孟州,181108;鹤壁,181201;浚县,181202;淇县,181203;濮阳,181301;台前,181302;南乐,181303;清丰,181304;范县,181305;周口,181401;扶沟,181402;太康,181403;淮阳,181404;西华,181405;商水,181406;项城,181407;郸城,181408;鹿邑,181409;沈丘,181410;黄泛区,181411;漯河,181501;临颍,181502;舞阳,181503;驻马店,181601;西平,181602;遂平,181603;上蔡,181604;汝南,181605;泌阳,181606;平舆,181607;新蔡,181608;确山,181609;正阳,181610;三门峡,181701;灵宝,181702;渑池,181703;卢氏,181704;济源,181801;南京,190101;溧水,190102;高淳,190103;江宁,190104;六合,190105;江浦,190106;浦口,190107;无锡,190201;江阴,190202;宜兴,190203;镇江,190301;丹阳,190302;扬中,190303;句容,190304;丹徒,190305;苏州,190401;常熟,190402;张家港,190403;昆山,190404;吴县东山,190405;吴县,190406;吴江,190407;太仓,190408;南通,190501;海安,190502;如皋,190503;如东,190504;吕泗,190505;吕泗渔场,190506;启东,190507;海门,190508;通州,190509;扬州,190601;宝应,190602;仪征,190603;高邮,190604;江都,190605;邗江,190606;盐城,190701;响水,190702;滨海,190703;阜宁,190704;射阳,190705;建湖,190706;东台,190707;大丰,190708;盐都,190709;徐州,190801;徐州农试站,190802;丰县,190803;沛县,190804;邳州,190805;睢宁,190806;新沂,190807;淮安,190901;金湖,190902;盱眙,190903;洪泽,190904;涟水,190905;淮阴县,190906;淮阴,190907;楚州,190908;连云港,191001;东海,191002;赣榆,191003;灌云,191004;灌南,191005;西连岛,191006;燕尾港,191007;常州,191101;溧阳,191102;金坛,191103;泰州,191201;兴化,191202;泰兴,191203;姜堰,191204;靖江,191205;宿迁,191301;沭阳,191302;泗阳,191303;泗洪,191304;武汉,200101;蔡甸,200102;黄陂,200103;新洲,200104;江夏,200105;襄樊,200201;襄阳,200202;保康,200203;南漳,200204;宜城,200205;老河口,200206;谷城,200207;枣阳,200208;鄂州,200301;孝感,200401;安陆,200402;云梦,200403;大悟,200404;应城,200405;汉川,200406;黄冈,200501;红安,200502;麻城,200503;罗田,200504;英山,200505;浠水,200506;蕲春,200507;黄梅,200508;武穴,200509;黄石,200601;大冶,200602;阳新,200603;咸宁,200701;赤壁,200702;嘉鱼,200703;崇阳,200704;通城,200705;通山,200706;荆州,200801;江陵,200802;公安,200803;石首,200804;监利,200805;洪湖,200806;松滋,200807;宜昌,200901;远安,200902;秭归,200903;兴山,200904;宜昌县,200905;五峰,200906;当阳,200907;长阳,200908;宜都,200909;枝江,200910;三峡,200911;夷陵,200912;恩施,201001;利川,201002;建始,201003;咸丰,201004;宣恩,201005;鹤峰,201006;来凤,201007;巴东,201008;绿葱坡,201009;十堰,201101;竹溪,201102;郧西,201103;郧县,201104;竹山,201105;房县,201106;丹江口,201107;神农架,201201;随州,201301;广水,201302;荆门,201401;钟祥,201402;京山,201403;天门,201501;仙桃,201601;潜江,201701;杭州,210101;萧山,210102;桐庐,210103;淳安,210104;建德,210105;余杭,210106;临安,210107;富阳,210108;湖州,210201;长兴,210202;安吉,210203;德清,210204;嘉兴,210301;嘉善,210302;海宁,210303;桐乡,210304;平湖,210305;海盐,210306;宁波,210401;慈溪,210403;余姚,210404;奉化,210405;象山,210406;石浦,210407;宁海,210408;鄞县,210409;北仑,210410;鄞州,210411;镇海,210412;绍兴,210501;诸暨,210502;上虞,210503;新昌,210504;嵊州,210505;台州,210601;括苍山,210602;玉环,210603;三门,210604;天台,210605;仙居,210606;温岭,210607;大陈,210608;洪家,210609;温州,210701;泰顺,210702;文成,210703;平阳,210704;瑞安,210705;洞头,210706;乐清,210707;永嘉,210708;苍南,210709;丽水,210801;遂昌,210802;龙泉,210803;缙云,210804;青田,210805;云和,210806;庆元,210807;金华,210901;浦江,210902;兰溪,210903;义乌,210904;东阳,210905;武义,210906;永康,210907;磐安,210908;衢州,211001;常山,211002;开化,211003;龙游,211004;江山,211005;舟山,211101;嵊泗,211102;嵊山,211103;岱山,211104;普陀,211105;定海,211106;合肥,220101;长丰,220102;肥东,220103;肥西,220104;蚌埠,220201;怀远,220202;固镇,220203;五河,220204;芜湖,220301;繁昌,220302;芜湖县,220303;南陵,220304;淮南,220401;凤台,220402;马鞍山,220501;当涂,220502;安庆,220601;枞阳,220602;太湖,220603;潜山,220604;怀宁,220605;宿松,220606;望江,220607;岳西,220608;桐城,220609;宿州,220701;砀山,220702;灵璧,220703;泗县,220704;萧县,220705;阜阳,220801;阜南,220802;颍上,220803;临泉,220804;界首,220805;太和,220806;亳州,220901;涡阳,220902;利辛,220903;蒙城,220904;黄山站,221001;黄山区,221002;屯溪,221003;祁门,221004;黟县,221005;歙县,221006;休宁,221007;黄山市,221008;滁州,221101;凤阳,221102;明光,221103;定远,221104;全椒,221105;来安,221106;天长,221107;淮北,221201;濉溪,221202;铜陵,221301;宣城,221401;泾县,221402;旌德,221403;宁国,221404;绩溪,221405;广德,221406;郎溪,221407;六安,221501;霍邱,221502;寿县,221503;南溪,221504;金寨,221505;霍山,221506;舒城,221507;巢湖,221601;庐江,221602;无为,221603;含山,221604;和县,221605;池州,221701;东至,221702;青阳,221703;九华山,221704;石台,221705;福州,230101;闽清,230102;闽侯,230103;罗源,230104;连江,230105;马祖,230106;永泰,230107;平潭,230108;福州郊区,230109;长乐,230110;福清,230111;平潭海峡大桥,230112;厦门,230201;同安,230202;宁德,230301;古田,230302;霞浦,230303;寿宁,230304;周宁,230305;福安,230306;柘荣,230307;福鼎,230308;屏南,230309;莆田,230401;仙游,230402;秀屿港,230403;泉州,230501;安溪,230502;九仙山,230503;永春,230504;德化,230505;南安,230506;崇武,230507;金山,230508;晋江,230509;漳州,230601;长泰,230602;南靖,230603;平和,230604;龙海,230605;漳浦,230606;诏安,230607;东山,230608;云霄,230609;华安,230610;龙岩,230701;长汀,230702;连城,230703;武平,230704;上杭,230705;永定,230706;漳平,230707;三明,230801;宁化,230802;清流,230803;泰宁,230804;将乐,230805;建宁,230806;明溪,230807;沙县,230808;尤溪,230809;永安,230810;大田,230811;南平,230901;顺昌,230902;光泽,230903;邵武,230904;武夷山,230905;浦城,230906;建阳,230907;松溪,230908;政和,230909;建瓯,230910;南昌,240101;新建,240102;南昌县,240103;安义,240104;进贤,240105;莲塘,240106;九江,240201;瑞昌,240202;庐山,240203;武宁,240204;德安,240205;永修,240206;湖口,240207;彭泽,240208;星子,240209;都昌,240210;棠荫,240211;修水,240212;上饶,240301;鄱阳,240302;婺源,240303;康山,240304;余干,240305;万年,240306;德兴,240307;上饶县,240308;弋阳,240309;横峰,240310;铅山,240311;玉山,240312;广丰,240313;波阳,240314;抚州,240401;广昌,240402;乐安,240403;崇仁,240404;金溪,240405;资溪,240406;宜黄,240407;南城,240408;南丰,240409;黎川,240410;东乡,240411;宜春,240501;铜鼓,240502;宜丰,240503;万载,240504;上高,240505;靖安,240506;奉新,240507;高安,240508;樟树,240509;丰城,240510;吉安,240601;吉安县,240602;吉水,240603;新干,240604;峡江,240605;永丰,240606;永新,240607;井冈山,240608;万安,240609;遂川,240610;泰和,240611;安福,240612;宁冈,240613;赣州,240701;崇义,240702;上犹,240703;南康,240704;大余,240705;信丰,240706;宁都,240707;石城,240708;瑞金,240709;于都,240710;会昌,240711;安远,240712;全南,240713;龙南,240714;定南,240715;寻乌,240716;兴国,240717;景德镇,240801;乐平,240802;萍乡,240901;莲花,240902;新余,241001;分宜,241002;鹰潭,241101;余江,241102;贵溪,241103;长沙,250101;宁乡,250102;浏阳,250103;马坡岭,250104;湘潭,250201;韶山,250202;湘乡,250203;株洲,250301;攸县,250302;醴陵,250303;株洲县,250304;茶陵,250305;炎陵,250306;衡阳,250401;衡山,250402;衡东,250403;祁东,250404;衡阳县,250405;常宁,250406;衡南,250407;耒阳,250408;南岳,250409;郴州,250501;桂阳,250502;嘉禾,250503;宜章,250504;临武,250505;桥口,250506;资兴,250507;汝城,250508;安仁,250509;永兴,250510;桂东,250511;常德,250601;安乡,250602;桃源,250603;汉寿,250604;澧县,250605;临澧,250606;石门,250607;益阳,250700;赫山区,250701;南县,250702;桃江,250703;安化,250704;沅江,250705;娄底,250801;双峰,250802;冷水江,250803;冷水滩,250804;新化,250805;涟源,250806;邵阳,250901;隆回,250902;洞口,250903;新邵,250904;邵东,250905;绥宁,250906;新宁,250907;武冈,250908;城步,250909;邵阳县,250910;岳阳,251001;华容,251002;湘阴,251003;汨罗,251004;平江,251005;临湘,251006;张家界,251101;桑植,251102;慈利,251103;怀化,251201;鹤城区,251202;沅陵,251203;辰溪,251204;靖州,251205;会同,251206;通道,251207;麻阳,251208;新晃,251209;芷江,251210;溆浦,251211;黔阳,251301;洪江,251302;永州,251401;祁阳,251402;东安,251403;双牌,251404;道县,251405;宁远,251406;江永,251407;蓝山,251408;新田,251409;江华,251410;吉首,251501;保靖,251502;永顺,251503;古丈,251504;凤凰,251505;泸溪,251506;龙山,251507;花垣,251508;贵阳,260101;白云,260102;花溪,260103;乌当,260104;息烽,260105;开阳,260106;修文,260107;清镇,260108;遵义,260201;遵义县,260202;仁怀,260203;绥阳,260204;湄潭,260205;凤冈,260206;桐梓,260207;赤水,260208;习水,260209;道真,260210;正安,260211;务川,260212;余庆,260213;汇川,260214;安顺,260301;普定,260302;镇宁,260303;平坝,260304;紫云,260305;关岭,260306;都匀,260401;贵定,260402;瓮安,260403;长顺,260404;福泉,260405;惠水,260406;龙里,260407;罗甸,260408;平塘,260409;独山,260410;三都,260411;荔波,260412;凯里,260501;岑巩,260502;施秉,260503;镇远,260504;黄平,260505;黄平旧洲,260506;麻江,260507;丹寨,260508;三穗,260509;台江,260510;剑河,260511;雷山,260512;黎平,260513;天柱,260514;锦屏,260515;榕江,260516;从江,260517;炉山,260518;铜仁,260601;江口,260602;玉屏,260603;万山,260604;思南,260605;塘头,260606;印江,260607;石阡,260608;沿河,260609;德江,260610;松桃,260611;毕节,260701;赫章,260702;金沙,260703;威宁,260704;大方,260705;纳雍,260706;织金,260707;六盘水,260801;六枝,260802;水城,260803;盘县,260804;黔西,260901;晴隆,260902;兴仁,260903;贞丰,260904;望谟,260905;兴义,260906;安龙,260907;册亨,260908;普安,260909;成都,270101;龙泉驿,270102;新都,270103;温江,270104;金堂,270105;双流,270106;郫县,270107;大邑,270108;蒲江,270109;新津,270110;都江堰,270111;彭州,270112;邛崃,270113;崇州,270114;崇庆,270115;彭县,270116;攀枝花,270201;仁和,270202;米易,270203;盐边,270204;自贡,270301;富顺,270302;荣县,270303;绵阳,270401;三台,270402;盐亭,270403;安县,270404;梓潼,270405;北川,270406;平武,270407;江油,270408;南充,270501;南部,270502;营山,270503;蓬安,270504;仪陇,270505;西充,270506;阆中,270507;达州,270601;宣汉,270602;开江,270603;大竹,270604;渠县,270605;万源,270606;达川,270607;遂宁,270701;蓬溪,270702;射洪,270703;广安,270801;岳池,270802;武胜,270803;邻水,270804;华蓥山,270805;巴中,270901;通江,270902;南江,270903;平昌,270904;泸州,271001;泸县,271003;合江,271004;叙永,271005;古蔺,271006;纳溪,271007;宜宾,271101;宜宾农试站,271102;宜宾县,271103;南溪,271104;江安,271105;长宁,271106;高县,271107;珙县,271108;筠连,271109;兴文,271110;屏山,271111;内江,271201;东兴,271202;威远,271203;资中,271204;隆昌,271205;资阳,271301;安岳,271302;乐至,271303;简阳,271304;乐山,271401;犍为,271402;井研,271403;夹江,271404;沐川,271405;峨边,271406;马边,271407;峨眉,271408;峨眉山,271409;眉山,271501;仁寿,271502;彭山,271503;洪雅,271504;丹棱,271505;青神,271506;凉山,271601;木里,271603;盐源,271604;德昌,271605;会理,271606;会东,271607;宁南,271608;普格,271609;西昌,271610;金阳,271611;昭觉,271612;喜德,271613;冕宁,271614;越西,271615;甘洛,271616;雷波,271617;美姑,271618;布拖,271619;雅安,271701;名山,271702;荣经,271703;汉源,271704;石棉,271705;天全,271706;芦山,271707;宝兴,271708;甘孜,271801;康定,271802;泸定,271803;丹巴,271804;九龙,271805;雅江,271806;道孚,271807;炉霍,271808;新龙,271809;德格,271810;白玉,271811;石渠,271812;色达,271813;理塘,271814;巴塘,271815;乡城,271816;稻城,271817;得荣,271818;阿坝,271901;汶川,271902;理县,271903;茂县,271904;松潘,271905;九寨沟,271906;金川,271907;小金,271908;黑水,271909;马尔康,271910;壤塘,271911;若尔盖,271912;红原,271913;南坪,271914;德阳,272001;中江,272002;广汉,272003;什邡,272004;绵竹,272005;罗江,272006;广元,272101;旺苍,272102;青川,272103;剑阁,272104;苍溪,272105;广州,280101;番禺,280102;从化,280103;增城,280104;花都,280105;天河,280106;韶关,280201;乳源,280202;始兴,280203;翁源,280204;乐昌,280205;仁化,280206;南雄,280207;新丰,280208;曲江,280209;惠州,280301;博罗,280302;惠阳,280303;惠东,280304;龙门,280305;梅州,280401;兴宁,280402;蕉岭,280403;大埔,280404;丰顺,280406;平远,280407;五华,280408;梅县,280409;汕头,280501;潮阳,280502;澄海,280503;南澳,280504;云澳,280505;南澎岛,280506;深圳,280601;珠海,280701;斗门,280702;黄茅洲,280703;佛山,280800;顺德,280801;三水,280802;南海,280803;肇庆,280901;广宁,280902;四会,280903;德庆,280905;怀集,280906;封开,280907;高要,280908;湛江,281001;吴川,281002;雷州,281003;徐闻,281004;廉江,281005;硇洲,281006;遂溪,281007;江门,281101;开平,281103;新会,281104;恩平,281105;台山,281106;上川岛,281107;鹤山,281108;河源,281201;紫金,281202;连平,281203;和平,281204;龙川,281205;清远,281301;连南,281302;连州,281303;连山,281304;阳山,281305;佛冈,281306;英德,281307;云浮,281401;罗定,281402;新兴,281403;郁南,281404;潮州,281501;饶平,281502;东莞,281601;中山,281701;阳江,281801;阳春,281802;揭阳,281901;揭西,281902;普宁,281903;惠来,281904;茂名,282001;高州,282002;化州,282003;电白,282004;信宜,282005;汕尾,282101;海丰,282102;陆丰,282103;遮浪,282104;东沙岛,282105;昆明,290101;昆明农试站,290102;东川,290103;寻甸,290104;晋宁,290105;宜良,290106;石林,290107;呈贡,290108;富民,290109;嵩明,290110;禄劝,290111;安宁,290112;太华山,290113;河口,290114;大理,290201;云龙,290202;漾鼻,290203;永平,290204;宾川,290205;弥渡,290206;祥云,290207;魏山,290208;剑川,290209;洱源,290210;鹤庆,290211;南涧,290212;红河,290301;石屏,290302;建水,290303;弥勒,290304;元阳,290305;绿春,290306;开远,290307;个旧,290308;蒙自,290309;屏边,290310;泸西,290311;金平,290312;曲靖,290401;沾益,290402;陆良,290403;富源,290404;马龙,290405;师宗,290406;罗平,290407;会泽,290408;宣威,290409;保山,290501;富宁,290502;龙陵,290503;施甸,290504;昌宁,290505;腾冲,290506;文山,290601;西畴,290602;马关,290603;麻栗坡,290604;砚山,290605;邱北,290606;广南,290607;玉溪,290701;澄江,290702;江川,290703;通海,290704;华宁,290705;新平,290706;易门,290707;峨山,290708;元江,290709;楚雄,290801;大姚,290802;元谋,290803;姚安,290804;牟定,290805;南华,290806;武定,290807;禄丰,290808;双柏,290809;永仁,290810;普洱,290901;景谷,290902;景东,290903;澜沧,290904;普洱,290905;墨江,290906;江城,290907;孟连,290908;西盟,290909;镇源,290910;镇沅,290911;宁洱,290912;昭通,291001;鲁甸,291002;彝良,291003;镇雄,291004;威信,291005;巧家,291006;绥江,291007;永善,291008;盐津,291009;大关,291010;临沧,291101;沧源,291102;耿马,291103;双江,291104;凤庆,291105;永德,291106;云县,291107;镇康,291108;怒江,291201;福贡,291203;兰坪,291204;泸水,291205;六库,291206;贡山,291207;香格里拉,291301;德钦,291302;维西,291303;中甸,291304;丽江,291401;永胜,291402;华坪,291403;宁蒗,291404;德宏,291501;潞江坝,291502;陇川,291503;盈江,291504;畹町镇,291505;瑞丽,291506;梁河,291507;潞西,291508;景洪,291601;大勐龙,291602;勐海,291603;景洪电站,291604;勐腊,291605;南宁,300101;南宁城区,300102;邕宁,300103;横县,300104;隆安,300105;马山,300106;上林,300107;武鸣,300108;宾阳,300109;硕龙,300110;崇左,300201;天等,300202;龙州,300203;凭祥,300204;大新,300205;扶绥,300206;宁明,300207;海渊,300208;柳州,300301;柳城,300302;沙塘,300303;鹿寨,300304;柳江,300305;融安,300306;融水,300307;三江,300308;来宾,300401;忻城,300402;金秀,300403;象州,300404;武宣,300405;桂林,300501;桂林农试站,300502;龙胜,300503;永福,300504;临桂,300505;兴安,300506;灵川,300507;全州,300508;灌阳,300509;阳朔,300510;恭城,300511;平乐,300512;荔浦,300513;资源,300514;梧州,300601;藤县,300602;太平,300603;苍梧,300604;蒙山,300605;岑溪,300606;贺州,300701;昭平,300702;富川,300703;钟山,300704;信都,300705;贵港,300801;桂平,300802;平南,300803;玉林,300901;博白,300902;北流,300903;容县,300904;陆川,300905;百色,301001;那坡,301002;田阳,301003;德保,301004;靖西,301005;田东,301006;平果,301007;隆林,301008;西林,301009;乐业,301010;凌云,301011;田林,301012;钦州,301101;浦北,301102;灵山,301103;河池,301201;天峨,301202;东兰,301203;巴马,301204;环江,301205;罗城,301206;宜州,301207;凤山,301208;南丹,301209;都安,301210;北海,301301;合浦,301302;涠洲岛,301303;防城港,301401;上思,301402;东兴,301403;板栏,301404;防城,301405;海口,310101;琼山,310102;三亚,310201;东方,310202;临高,310203;澄迈,310204;儋州,310205;昌江,310206;白沙,310207;琼中,310208;定安,310209;屯昌,310210;琼海,310211;文昌,310212;清兰,310213;保亭,310214;万宁,310215;陵水,310216;西沙,310217;珊瑚岛,310218;永署礁,310219;南沙岛,310220;乐东,310221;五指山,310222;通什,310223;香港,320101;九龙,320102;新界,320103;中环,320104;铜锣湾,320105;澳门,330101;台北县,340101;台北市,340102;高雄,340201;东港,340202;大武,340203;恒春,340204;兰屿,340205;台南,340301;台中,340401;桃园,340501;新竹县,340601;新竹市,340602;公馆,340603;宜兰,340701;马公,340801;东吉屿,340802;嘉义,340901;阿里山,340902;玉山,340903;新港,340904`.split(';').forEach(city => {
        const [name, id] = city.split(',');
        result[name] = id;
    });
    return result;
})();

// 获取页面元素
const $tip = document.getElementById("tip");
const $cityid = document.getElementById("cityid");
const $wifiName = document.getElementById("wifi-name");
const $wifiPassword = document.getElementById("wifi-password");
const $cityName = document.getElementById("city-name");
const $deviceName = document.getElementById("device-name");
const $saveButton = document.getElementById("save-button");
// 更新全局提示
function updateTip(message, isError = true) {
    $tip.innerText = message;
    $tip.classList.toggle("error", isError);
}
function toggleAdvancedSettings() {
            const settings = document.querySelector('.advanced-settings');
            const arrow = document.querySelector('.advanced-toggle button');
            settings.style.display = settings.style.display === 'none' ? 'block' : 'none';
            arrow.innerHTML = settings.style.display === 'none' ? '⯆ 高级设置（小白勿改）' : '⯈ 高级设置（小白勿改）';
}
function checkForm() {
    let isValid = true;

    // 检查 WiFi 名称
    const wifiName = $wifiName.value.trim();
    if (!wifiName) {
        updateTip("请输入WiFi名称");
        isValid = false;
    }

    // 检查 WiFi 密码（仅在未输入城市名称时检查）
    const wifiPassword = $wifiPassword.value.trim();
    if (!wifiPassword && !$cityName.value.trim()) {
        updateTip("请输入WiFi密码");
        isValid = false;
    }

    // 检查城市名称
    const cityName = $cityName.value.trim();
    if (!cityName) {
        updateTip("请输入城市名称");
        isValid = false;
    } else {
        const code = cityIds[cityName];
        if (!code) {
            updateTip("未找到城市代码");
            isValid = false;
        } else {
            $cityid.value = "101" + code;
        }
    }

    // 检查设备名称
    const deviceName = $deviceName.value.trim();
    if (!deviceName) {
        updateTip("请输入设备名称");
        isValid = false;
    } else if (!/^[a-zA-Z0-9]+$/.test(deviceName)) {
        updateTip("设备名称只能包含英文和数字");
        isValid = false;
    } else if (deviceName.length > 8) {
        updateTip("设备名称长度不能超过8个字符");
        isValid = false;
    }

    // 检查性别选择
    const genderSelected = document.querySelector('input[name="gender"]:checked');
    if (!genderSelected) {
        updateTip("请选择性别");
        isValid = false;
    }

    // 根据验证结果更新按钮状态
    if (isValid) {
        updateTip("所有输入有效", false);
        $saveButton.disabled = false;
        $saveButton.style.backgroundColor = "green";
        $saveButton.style.cursor = "pointer";
    } else {
        $saveButton.disabled = true;
        $saveButton.style.backgroundColor = ""; // 恢复到原来的背景颜色
        $saveButton.style.cursor = "default";
    }
}

// 监听输入事件，实时验证表单
$wifiName.addEventListener("input", checkForm);
$wifiPassword.addEventListener("input", checkForm);
$cityName.addEventListener("input", checkForm);
$deviceName.addEventListener("input", checkForm);

// 监听性别选择框的变化
const genderOptions = document.querySelectorAll('input[name="gender"]');
genderOptions.forEach(option => option.addEventListener("change", checkForm));

document.addEventListener('DOMContentLoaded', () => {
    const advancedButton = document.getElementById('advanced-settings-button');
    const advancedSettings = document.getElementById('advanced-settings');

    advancedButton.addEventListener('click', () => {
        // 切换显示/隐藏状态
        advancedSettings.classList.toggle('active');
    });
});

)rawliteral";


const byte DNS_PORT = 53;//DNS端口号
IPAddress apIP(192, 168, 4, 1);//esp32-AP-IP地址
DNSServer dnsServer;//创建dnsServer实例
WebServer server(80);//创建WebServer
extern WiFi_Info wifi_info_list[5];  // 支持最多存储5组WiFi信息
extern const int wifi_info_count;    // 确保在此声明



void handleRoot() {
  // 动态生成SSID选项和设备名称
  String finalPage = String(page_header) + "%SSID_OPTIONS%" + page_footer;
  finalPage.replace("%SSID_OPTIONS%", generateSSIDOptions());
  std::string portStr = std::to_string(PORT);
  const char* port_value = portStr.c_str() ? portStr.c_str() : ""; 
  Serial.println("port_value:");
  Serial.println(port_value);
  Serial.println("port:");
  Serial.println(PORT);
  // 插入设备名称
  finalPage += device_value;
  finalPage += page_footer_2;
  finalPage += mqtt_url_value;
  finalPage += page_footer_3;
  finalPage += port_value;
  finalPage += page_footer_4;
  finalPage += mqtt_user_value;
  finalPage += page_footer_5;
  finalPage += mqtt_pass_value;
  finalPage += page_footer_6;

  server.send(200, "text/html", finalPage);
}


void handleStyleCss() {// style.css
  server.send(200, "text/css", style_css);
}

void handleIndexJs() {// index.js
  server.send(200, "application/javascript", index_js);
}

void handleRootPost() {
  Serial.println("handleRootPost");
  bool isFemale = 0;
  // 检查是否有 ssid 参数
  if (server.hasArg("ssid")) {
    Serial.print("got ssid:");
    strcpy(sta_ssid, server.arg("ssid").c_str());
    Serial.println(sta_ssid);
  } 
  // 检查是否有 password 参数
  if (server.hasArg("password")) {
    Serial.print("got password:");
    strcpy(sta_password, server.arg("password").c_str());
    Serial.println(sta_password);
  } 
  // 检查是否有 citycode 参数
  if (server.hasArg("cityid")) {
    Serial.print("got citycode:");
    strcpy(citycode, server.arg("cityid").c_str());
    Serial.println(citycode);
  } 
    // 检查是否有 citycode 参数
  if (server.hasArg("deviceName")) {
    Serial.print("got deviceName:");
    strcpy(device_name, server.arg("deviceName").c_str());
    Serial.println(device_name);
  } 
    // 检查是否有 gender 参数
  if (server.hasArg("gender")) {
    String gender = server.arg("gender");
    isFemale = (gender == "1");
    Serial.print("got gender: ");
    Serial.println(gender);
  }
  //检查是否有 mqtt_url参数
    if (server.hasArg("mqtt_url")) {
    strcpy(MQTT_IP, server.arg("mqtt_url").c_str());
    Serial.print("got mqtturl: ");
    Serial.println(MQTT_IP);
  }
    if (server.hasArg("port")) {
    String portString = server.arg("port");
    PORT = portString.toInt();
    // 检查转换是否成功（确保值在 uint16_t 范围内）
    if (PORT > 0 && PORT <= 65535) {
        Serial.println("Port successfully assigned: " + String(PORT));
    } else {
        Serial.println("Invalid port value!");
    }
  }
    //检查是否有 mqtt_url参数
    if (server.hasArg("mqtt_user")) {
    strcpy(MQTT_USER, server.arg("mqtt_user").c_str());
    Serial.print("got mqtt_user: ");
    Serial.println(MQTT_USER);
  }
    //检查是否有 mqtt_url参数
    if (server.hasArg("mqtt_pass")) {
    strcpy(MQTT_PASSWORD, server.arg("mqtt_pass").c_str());
    Serial.print("got mqtt_pass: ");
    Serial.println(MQTT_PASSWORD);
  }
  // 保存新的 WiFi 信息到 Preferences
  preferences.begin("wifi", false);
  bool saved = false;
  for (int i = 0; i < wifi_info_count; i++) {
    if (strlen(wifi_info_list[i].ssid) == 0) {
      preferences.putString(("ssid" + String(i)).c_str(), sta_ssid);
      preferences.putString(("password" + String(i)).c_str(), sta_password);
      saved = true;
      break;
    }
  }
  
  if (!saved) {
    // 如果存储已满，则覆盖第一个
    preferences.putString("ssid0", sta_ssid);
    preferences.putString("password0", sta_password);
  }
  
  preferences.putString("citycode", citycode);
  preferences.putString("devicename", device_name);
  preferences.putBool("gender", isFemale);
  preferences.putString("mqtt_ip", MQTT_IP);
  preferences.putUInt("mqtt_port", PORT);
  preferences.putString("mqtt_user", MQTT_USER);
  preferences.putString("mqtt_pass", MQTT_PASSWORD);
 
  preferences.end();

  server.send(200, "text/html", "<meta charset='UTF-8'>保存成功，情侣对讲机重启中...");
  delay(2000);

  // 重新启动设备以应用新的设置
  ESP.restart();
}


void initBasic(void){//初始化基础
  WiFi.hostname("Smart-ESP32");//设置ESP32设备名
}

void initSoftAP(void){//初始化AP模式
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  if(WiFi.softAP(AP_NAME)){
    Serial.println("ESP32 SoftAP is right");
  }
}

void initWebServer(void){//初始化WebServer
        // 将 PORT 转换为字符串

  server.on("/", HTTP_GET, handleRoot);//设置主页回调函数
  server.on("/index.js", HTTP_GET, handleIndexJs);
  server.on("/style.css", HTTP_GET, handleStyleCss);
  server.onNotFound(handleRoot);//设置无法响应的http请求的回调函数
  server.on("/", HTTP_POST, handleRootPost);//设置Post请求回调函数
  server.begin();//启动WebServer
  Serial.println("WebServer started!");
}

void initDNS(void){//初始化DNS服务器
  if(dnsServer.start(DNS_PORT, "*", apIP)){//判断将所有地址映射到esp32的ip上是否成功
    Serial.println("start dnsserver success.");
  }
  else Serial.println("start dnsserver failed.");
}


// 函数声明
void handleRoot();
void handleStyleCss();
void handleIndexJs();
void handleRootPost();
void initWebServer();